﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Reporting.WinForms;

namespace GenericReport
{
    /// <summary>
    /// Interaction logic for Startup.xaml
    /// </summary>
    public partial class Startup : UserControl
    {
        public DataTable fieldsDataTable, dataDataTable;

        #region properties
        public static DependencyProperty reportCompanyProperty = DependencyProperty.Register("reportCompany", typeof(string), typeof(Startup), new PropertyMetadata(string.Empty));
        public string ReportCompanyProperty
        {
            get { return (string)GetValue(reportCompanyProperty); }
            set { SetValue(reportCompanyProperty, value); }
        }
        public static DependencyProperty reportTitleProperty = DependencyProperty.Register("reportTitle", typeof(string), typeof(Startup), new PropertyMetadata(string.Empty));
        public string ReportTitleProperty
        {
            get { return (string)GetValue(reportTitleProperty); }
            set { SetValue(reportTitleProperty, value); }
        }
        public static DependencyProperty criteriaProperty = DependencyProperty.Register("criteria", typeof(string), typeof(Startup), new PropertyMetadata(string.Empty));
        public string CriteriaProperty
        {
            get { return (string)GetValue(criteriaProperty); }
            set { SetValue(criteriaProperty, value); }
        }
        public static DependencyProperty sortingProperty = DependencyProperty.Register("sorting", typeof(string), typeof(Startup), new PropertyMetadata(string.Empty));
        public string SortingProperty
        {
            get { return (string)GetValue(sortingProperty); }
            set { SetValue(sortingProperty, value); }
        }
        public static DependencyProperty orientationProperty = DependencyProperty.Register("orientation", typeof(string), typeof(Startup), new PropertyMetadata(string.Empty));
        public string OrientationProperty
        {
            get { return (string)GetValue(orientationProperty); }
            set { SetValue(orientationProperty, value); }
        }
        public static readonly DependencyProperty tableBorderProperty = DependencyProperty.Register("tableBorder", typeof(bool), typeof(Startup), new PropertyMetadata(false));
        public bool TableBorderProperty
        {
            get { return (bool)GetValue(tableBorderProperty); }
            set { SetValue(tableBorderProperty, value); }
        }
        public static readonly DependencyProperty gridlinesProperty = DependencyProperty.Register("gridlines", typeof(bool), typeof(Startup), new PropertyMetadata(false));
        public bool GridlinesProperty
        {
            get { return (bool)GetValue(gridlinesProperty); }
            set { SetValue(gridlinesProperty, value); }
        }
        public static DependencyProperty alternatingRowColourProperty = DependencyProperty.Register("alternatingRowColour", typeof(string), typeof(Startup), new PropertyMetadata(string.Empty));
        public string AlternatingRowColourProperty
        {
            get { return (string)GetValue(alternatingRowColourProperty); }
            set { SetValue(alternatingRowColourProperty, value); }
        }
        public static DependencyProperty fieldWidthsProperty = DependencyProperty.Register("fieldWidths", typeof(string), typeof(Startup), new PropertyMetadata(string.Empty));
        public string FieldWidthsProperty
        {
            get { return (string)GetValue(fieldWidthsProperty); }
            set { SetValue(fieldWidthsProperty, value); }
        }
        public static DependencyProperty xmlSchemaProperty = DependencyProperty.Register("xmlSchema", typeof(string), typeof(Startup), new PropertyMetadata(string.Empty));
        public string XmlSchemaProperty
        {
            get { return (string)GetValue(xmlSchemaProperty); }
            set { SetValue(xmlSchemaProperty, value); }
        }
        public static DependencyProperty xmlDataProperty = DependencyProperty.Register("xmlData", typeof(string), typeof(Startup), new PropertyMetadata(string.Empty));
        public string XmlDataProperty
        {
            get { return (string)GetValue(xmlDataProperty); }
            set { SetValue(xmlDataProperty, value); }
        }
        public static DependencyProperty xmlFieldsProperty = DependencyProperty.Register("xmlFields", typeof(string), typeof(Startup), new PropertyMetadata(string.Empty));
        public string XmlFieldsProperty
        {
            get { return (string)GetValue(xmlFieldsProperty); }
            set { SetValue(xmlFieldsProperty, value); }
        }
        public static DependencyProperty groupByField1Property = DependencyProperty.Register("groupByField1", typeof(string), typeof(Startup), new PropertyMetadata(string.Empty));
        public string GroupByField1Property
        {
            get { return (string)GetValue(groupByField1Property); }
            set { SetValue(groupByField1Property, value); }
        }
        public static DependencyProperty groupByField2Property = DependencyProperty.Register("groupByField2", typeof(string), typeof(Startup), new PropertyMetadata(string.Empty));
        public string GroupByField2Property
        {
            get { return (string)GetValue(groupByField2Property); }
            set { SetValue(groupByField2Property, value); }
        }
        public static DependencyProperty groupByField3Property = DependencyProperty.Register("groupByField3", typeof(string), typeof(Startup), new PropertyMetadata(string.Empty));
        public string GroupByField3Property
        {
            get { return (string)GetValue(groupByField3Property); }
            set { SetValue(groupByField3Property, value); }
        }
        public static DependencyProperty groupByField4Property = DependencyProperty.Register("groupByField4", typeof(string), typeof(Startup), new PropertyMetadata(string.Empty));
        public string GroupByField4Property
        {
            get { return (string)GetValue(groupByField4Property); }
            set { SetValue(groupByField4Property, value); }
        }
        public static DependencyProperty groupByField5Property = DependencyProperty.Register("groupByField5", typeof(string), typeof(Startup), new PropertyMetadata(string.Empty));
        public string GroupByField5Property
        {
            get { return (string)GetValue(groupByField5Property); }
            set { SetValue(groupByField5Property, value); }
        }
        public static DependencyProperty summariseField1Property = DependencyProperty.Register("summariseField1", typeof(string), typeof(Startup), new PropertyMetadata(string.Empty));
        public string SummariseField1Property
        {
            get { return (string)GetValue(summariseField1Property); }
            set { SetValue(summariseField1Property, value); }
        }
        public static DependencyProperty summariseField2Property = DependencyProperty.Register("summariseField2", typeof(string), typeof(Startup), new PropertyMetadata(string.Empty));
        public string SummariseField2Property
        {
            get { return (string)GetValue(summariseField2Property); }
            set { SetValue(summariseField2Property, value); }
        }
        public static DependencyProperty summariseField3Property = DependencyProperty.Register("summariseField3", typeof(string), typeof(Startup), new PropertyMetadata(string.Empty));
        public string SummariseField3Property
        {
            get { return (string)GetValue(summariseField3Property); }
            set { SetValue(summariseField3Property, value); }
        }
        public static DependencyProperty summariseField4Property = DependencyProperty.Register("summariseField4", typeof(string), typeof(Startup), new PropertyMetadata(string.Empty));
        public string SummariseField4Property
        {
            get { return (string)GetValue(summariseField4Property); }
            set { SetValue(summariseField4Property, value); }
        }
        public static DependencyProperty summariseField5Property = DependencyProperty.Register("summariseField5", typeof(string), typeof(Startup), new PropertyMetadata(string.Empty));
        public string SummariseField5Property
        {
            get { return (string)GetValue(summariseField5Property); }
            set { SetValue(summariseField5Property, value); }
        }
        public static readonly DependencyProperty successProperty = DependencyProperty.Register("success", typeof(bool), typeof(Startup), new PropertyMetadata(false));
        public bool SuccessProperty
        {
            get { return (bool)GetValue(successProperty); }
            set { SetValue(successProperty, value); }
        }
        public static readonly DependencyProperty errorMessageProperty = DependencyProperty.Register("errorMessage", typeof(string), typeof(Startup), new PropertyMetadata(string.Empty));
        public string ErrorMessageProperty
        {
            get { return (string)GetValue(errorMessageProperty); }
            set { SetValue(errorMessageProperty, value); }
        }
        public static readonly DependencyProperty stackTraceProperty = DependencyProperty.Register("stackTrace", typeof(string), typeof(Startup), new PropertyMetadata(string.Empty));
        public string StackTraceProperty
        {
            get { return (string)GetValue(stackTraceProperty); }
            set { SetValue(stackTraceProperty, value); }
        }
        #endregion

        public Startup()
        {
            InitializeComponent();
        }

        public void Process()
        {
            dataDataTable = new DataTable();
            fieldsDataTable = new DataTable();

            // define schema for field list
            fieldsDataTable.TableName = "dt";
            fieldsDataTable.Columns.Add("FieldName", typeof(string));
            fieldsDataTable.Columns.Add("FieldLabel", typeof(string));
            fieldsDataTable.Columns.Add("FieldType", typeof(string));
            fieldsDataTable.Columns.Add("FieldWidth", typeof(string));
            fieldsDataTable.Columns.Add("FieldTotal", typeof(bool));

            // attempt to load the data table schema
            try
            {
                dataDataTable.ReadXmlSchema(new StringReader(this.XmlSchemaProperty));
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to load xml schema.", "Generic Report");
                this.SuccessProperty = false;
                this.ErrorMessageProperty = ex.Message;
                this.StackTraceProperty = ex.StackTrace;
                return;
            }

            // attempt to load the data table
            try
            {
                dataDataTable.ReadXml(new StringReader(this.XmlDataProperty));
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to load xml data.", "Generic Report");
                this.SuccessProperty = false;
                this.ErrorMessageProperty = ex.Message;
                this.StackTraceProperty = ex.StackTrace;
                return;
            }

            // attempt to load field data
            try
            {
                fieldsDataTable.ReadXmlSchema(new StringReader(this.XmlFieldsProperty));
                fieldsDataTable.ReadXml(new StringReader(this.XmlFieldsProperty));
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to load xml field defintions.", "Generic Report");
                this.SuccessProperty = false;
                this.ErrorMessageProperty = ex.Message;
                this.StackTraceProperty = ex.StackTrace;
                return;
            }

            // update with any user-defined column widths
            char[] deliminters = new char[] { ',' };
            string[] parts = this.FieldWidthsProperty.Split(deliminters, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < parts.Length; i++)
            {
                fieldsDataTable.Rows[i]["FieldWidth"] = parts[i];
            }

            // rename columns
            for (int i = 0; i < fieldsDataTable.Rows.Count; i++)
            {
                dataDataTable.Columns[i].ColumnName = fieldsDataTable.Rows[i]["FieldName"].ToString();
            }

            // attempt to render report
            try
            {
                string dsName = dataDataTable.TableName;
                ReportDataSource ds = new ReportDataSource(dsName, dataDataTable);
                ReportViewer1.Reset();
                ReportViewer1.LocalReport.DataSources.Add(ds);
                ReportViewer1.LocalReport.DisplayName = dsName;
                ReportViewer1.LocalReport.LoadReportDefinition(this.GenerateReport());
                ReportViewer1.RefreshReport();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to render report.", "Generic Report");
                this.SuccessProperty = false;
                this.ErrorMessageProperty = ex.Message;
                this.StackTraceProperty = ex.StackTrace;
                return;
            }
            
            this.SuccessProperty = true;
        }

        public Stream GenerateReport()
        {
            int textBoxCounter = 1;
            string xml = "";
            xml += @"<?xml version=""1.0"" encoding=""utf-8""?>
<Report xmlns:rd=""http://schemas.microsoft.com/SQLServer/reporting/reportdesigner"" xmlns:cl=""http://schemas.microsoft.com/sqlserver/reporting/2010/01/componentdefinition"" xmlns=""http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition"">
  <AutoRefresh>0</AutoRefresh>
  <DataSources>
    <DataSource Name=""dt"">
      <DataSourceReference>dt</DataSourceReference>
    </DataSource>
  </DataSources>
  <DataSets>
    <DataSet Name=""dt"">
      <Fields>";
            for (int i = 0; i < fieldsDataTable.Rows.Count; i++)
            {
                xml += @"
        <Field Name=""" + fieldsDataTable.Rows[i]["FieldName"] + @""">
          <DataField>" + fieldsDataTable.Rows[i]["FieldName"] + @"</DataField>
          <rd:TypeName>" + dataDataTable.Columns[i].DataType.ToString() + @"</rd:TypeName>
        </Field>";
            }
            xml += @"
      </Fields>
      <Query>
        <DataSourceName>dt</DataSourceName>
        <CommandText />
        <rd:DataSourceName>true</rd:DataSourceName>
      </Query>
    </DataSet>
  </DataSets>
  <ReportSections>
    <ReportSection>
      <Body>
        <ReportItems>
          <Tablix Name=""Tablix1"">
            <TablixBody>
              <TablixColumns>";
            for (int i = 0; i < fieldsDataTable.Rows.Count; i++)
            {
                xml += @"
                <TablixColumn>
                  <Width>" + fieldsDataTable.Rows[i]["FieldWidth"] + @"cm</Width>
                </TablixColumn>";
            }
            xml += @"
              </TablixColumns>
              <TablixRows>
                <TablixRow>
                  <Height>0.6cm</Height>
                  <TablixCells>";
            for (int i = 0; i < fieldsDataTable.Rows.Count; i++)
            {
                xml += @"
                    <TablixCell>
                      <CellContents>
                        <Textbox Name=""Textbox" + textBoxCounter.ToString() + @""">
                          <CanGrow>true</CanGrow>
                          <UserSort>
                            <SortExpression>=Fields!" + fieldsDataTable.Rows[i]["FieldName"] + @".Value</SortExpression>
                          </UserSort>
                          <KeepTogether>true</KeepTogether>
                          <Paragraphs>
                            <Paragraph>
                              <TextRuns>
                                <TextRun>
                                  <Value>" + fieldsDataTable.Rows[i]["FieldLabel"] + @"</Value>
                                  <Style>
                                    <FontSize>8pt</FontSize>
                                    <FontWeight>Bold</FontWeight>
                                  </Style>
                                </TextRun>
                              </TextRuns>
                              <Style />
                            </Paragraph>
                          </Paragraphs>
                          <rd:DefaultName>Textbox" + textBoxCounter.ToString() + @"</rd:DefaultName>
                          <Style>
                            <Border>
                              <Color>LightGrey</Color>
                              <Style>" + ((this.GridlinesProperty) ? "Solid" : "None") + @"</Style>
                            </Border>
                            <BackgroundColor>" + this.AlternatingRowColourProperty + @"</BackgroundColor>
                            <PaddingLeft>2pt</PaddingLeft>
                            <PaddingRight>2pt</PaddingRight>
                            <PaddingTop>2pt</PaddingTop>
                            <PaddingBottom>2pt</PaddingBottom>
                          </Style>
                        </Textbox>
                      </CellContents>
                    </TablixCell>";
                textBoxCounter++;
            }
            xml += @"
                  </TablixCells>
                </TablixRow>
                <TablixRow>
                  <Height>0.6cm</Height>
                  <TablixCells>";
            for (int i = 0; i < fieldsDataTable.Rows.Count; i++)
            {
                xml += @"
                    <TablixCell>
                      <CellContents>
                        <Textbox Name=""Textbox" + textBoxCounter.ToString() + @""">
                          <CanGrow>true</CanGrow>
                          <KeepTogether>true</KeepTogether>
                          <Paragraphs>
                            <Paragraph>
                              <TextRuns>
                                <TextRun>
                                  <Value>=Fields!" + fieldsDataTable.Rows[i]["FieldName"] + @".Value</Value>
                                  <Style>
                                    <FontSize>8pt</FontSize>";
                switch (fieldsDataTable.Rows[i]["FieldType"].ToString())
                {
                    case "int":
                        xml += @"<Format>#,0;(#,0)</Format>";
                        break;
                    case "decimal":
                        xml += @"<Format>#,0.00;(#,0.00)</Format>";
                        break;
                    case "dateTime":
                        xml += @"<Format>yyyy-MM-dd hh:mm:ss</Format>";
                        break;
                }
                xml += @"
                                  </Style>
                                </TextRun>
                              </TextRuns>
                              <Style />
                            </Paragraph>
                          </Paragraphs>
                          <rd:DefaultName>Textbox" + textBoxCounter.ToString() + @"</rd:DefaultName>
                          <Style>
                            <Border>
                              <Color>LightGrey</Color>
                              <Style>" + ((this.GridlinesProperty) ? "Solid" : "None") + @"</Style>
                            </Border>
                            <BackgroundColor>= IIf(RowNumber(Nothing) Mod 2 = 0, """ + this.AlternatingRowColourProperty + @""", ""Transparent"")</BackgroundColor>
                            <PaddingLeft>2pt</PaddingLeft>
                            <PaddingRight>2pt</PaddingRight>
                            <PaddingTop>2pt</PaddingTop>
                            <PaddingBottom>2pt</PaddingBottom>
                          </Style>
                        </Textbox>
                      </CellContents>
                    </TablixCell>";
                textBoxCounter++;
            }
            xml += @"
                  </TablixCells>
                </TablixRow>
                <TablixRow>
                  <Height>0.6cm</Height>
                  <TablixCells>";
            for (int i = 0; i < fieldsDataTable.Rows.Count; i++)
            {
                xml += @"
                    <TablixCell>
                      <CellContents>
                        <Textbox Name=""Textbox" + textBoxCounter.ToString() + @""">
                          <CanGrow>true</CanGrow>
                          <KeepTogether>true</KeepTogether>
                          <Paragraphs>
                            <Paragraph>
                              <TextRuns>
                                <TextRun>";
                if ((bool)fieldsDataTable.Rows[i]["FieldTotal"])
                    xml += @"<Value>=Sum(Fields!" + fieldsDataTable.Rows[i]["FieldName"] + @".Value)</Value>";
                else
                    xml += @"<Value />";
                xml += @"
                                  <Style>
                                    <FontSize>8pt</FontSize>
                                    <FontWeight>Bold</FontWeight>";
                switch (fieldsDataTable.Rows[i]["FieldType"].ToString())
                {
                    case "int":
                        xml += @"<Format>#,0;(#,0)</Format>";
                        break;
                    case "decimal":
                        xml += @"<Format>#,0.00;(#,0.00)</Format>";
                        break;
                }
                xml += @"
                                  </Style>
                                </TextRun>
                              </TextRuns>
                              <Style />
                            </Paragraph>
                          </Paragraphs>
                          <rd:DefaultName>Textbox" + textBoxCounter.ToString() + @"</rd:DefaultName>
                          <Style>
                            <Border>
                              <Color>LightGrey</Color>
                              <Style>" + ((this.GridlinesProperty) ? "Solid" : "None") + @"</Style>
                            </Border>
                            <BackgroundColor>= IIf(RowNumber(Nothing) Mod 2 = 0, """ + this.AlternatingRowColourProperty + @""", ""Transparent"")</BackgroundColor>
                            <PaddingLeft>2pt</PaddingLeft>
                            <PaddingRight>2pt</PaddingRight>
                            <PaddingTop>2pt</PaddingTop>
                            <PaddingBottom>2pt</PaddingBottom>
                          </Style>
                        </Textbox>
                      </CellContents>
                    </TablixCell>";
                textBoxCounter++;
            }
            xml += @"
                  </TablixCells>
                </TablixRow>
              </TablixRows>
            </TablixBody>
            <TablixColumnHierarchy>
              <TablixMembers>";
            for (int i = 0; i < fieldsDataTable.Rows.Count; i++)
            {
                xml += @"<TablixMember />";
            }
            xml += @"
              </TablixMembers>
            </TablixColumnHierarchy>
            <TablixRowHierarchy>
              <TablixMembers>
                <TablixMember>
                  <KeepWithGroup>After</KeepWithGroup>
                  <RepeatOnNewPage>true</RepeatOnNewPage>
                </TablixMember>
                <TablixMember>
                  <Group Name=""Details"" />
                </TablixMember>
                <TablixMember>
                  <KeepWithGroup>Before</KeepWithGroup>
                </TablixMember>
              </TablixMembers>
            </TablixRowHierarchy>
            <RepeatColumnHeaders>true</RepeatColumnHeaders>
            <DataSetName>dt</DataSetName>
            <Top>2.19157cm</Top>
            <Left>0cm</Left>
            <Height>1.8cm</Height>
            <Width>16.95916cm</Width>
            <Style>
              <Border>
                <Style>" + ((this.TableBorderProperty) ? "Solid" : "None") + @"</Style>
              </Border>
            </Style>
          </Tablix>
          <Textbox Name=""TextboxCriteria"">
            <CanGrow>true</CanGrow>
            <KeepTogether>true</KeepTogether>
            <Paragraphs>
              <Paragraph>
                <TextRuns>
                  <TextRun>
                    <Value>=Replace(""" + this.CriteriaProperty + @""",""|"",vbcrlf)</Value>
                    <Style>
                      <FontSize>8pt</FontSize>
                      <Color>Black</Color>
                    </Style>
                  </TextRun>
                </TextRuns>
                <Style />
              </Paragraph>
            </Paragraphs>
            <rd:DefaultName>TextboxCriteria</rd:DefaultName>
            <Top>0.6945cm</Top>
            <Left>0cm</Left>
            <Height>0.55562cm</Height>
            <Width>13.97cm</Width>
            <ZIndex>1</ZIndex>
            <Style>
              <Border>
                  <Color>LightGrey</Color>
                  <Style>None</Style>
              </Border>
              <PaddingLeft>2pt</PaddingLeft>
              <PaddingRight>2pt</PaddingRight>
              <PaddingTop>2pt</PaddingTop>
              <PaddingBottom>2pt</PaddingBottom>
            </Style>
          </Textbox>
          <Textbox Name=""TextboxCriteriaLabel"">
            <CanGrow>true</CanGrow>
            <KeepTogether>true</KeepTogether>
            <Paragraphs>
              <Paragraph>
                <TextRuns>
                  <TextRun>
                    <Value>Criteria</Value>
                    <Style>
                      <FontWeight>Bold</FontWeight>
                      <Color>Gray</Color>
                    </Style>
                  </TextRun>
                </TextRuns>
                <Style />
              </Paragraph>
            </Paragraphs>
            <rd:DefaultName>TextboxCriteriaLabel</rd:DefaultName>
            <Top>0cm</Top>
            <Left>0cm</Left>
            <Height>0.58867cm</Height>
            <Width>13.97cm</Width>
            <ZIndex>2</ZIndex>
            <Style>
              <Border>
                <Style>None</Style>
              </Border>
              <PaddingLeft>2pt</PaddingLeft>
              <PaddingRight>2pt</PaddingRight>
              <PaddingTop>2pt</PaddingTop>
              <PaddingBottom>2pt</PaddingBottom>
            </Style>
          </Textbox>
          <Textbox Name=""TextboxSorting"">
            <CanGrow>true</CanGrow>
            <KeepTogether>true</KeepTogether>
            <Paragraphs>
              <Paragraph>
                <TextRuns>
                  <TextRun>
                    <Value>=Replace(""" + this.SortingProperty + @""",""|"",vbcrlf)</Value>
                    <Style>
                      <FontSize>8pt</FontSize>
                      <Color>Black</Color>
                    </Style>
                  </TextRun>
                </TextRuns>
                <Style />
              </Paragraph>
            </Paragraphs>
            <rd:DefaultName>TextboxSorting</rd:DefaultName>
            <Top>0.6945cm</Top>
            <Left>14.14639cm</Left>
            <Height>0.55562cm</Height>
            <Width>13.28903cm</Width>
            <ZIndex>3</ZIndex>
            <Style>
              <Border>
                   <Color>LightGrey</Color>
                  <Style>None</Style>
              </Border>
              <PaddingLeft>2pt</PaddingLeft>
              <PaddingRight>2pt</PaddingRight>
              <PaddingTop>2pt</PaddingTop>
              <PaddingBottom>2pt</PaddingBottom>
            </Style>
          </Textbox>
          <Textbox Name=""TextboxSortingLable"">
            <CanGrow>true</CanGrow>
            <KeepTogether>true</KeepTogether>
            <Paragraphs>
              <Paragraph>
                <TextRuns>
                  <TextRun>
                    <Value>Sorting</Value>
                    <Style>
                      <FontWeight>Bold</FontWeight>
                      <Color>Gray</Color>
                    </Style>
                  </TextRun>
                </TextRuns>
                <Style />
              </Paragraph>
            </Paragraphs>
            <rd:DefaultName>TextboxSortingLabel</rd:DefaultName>
            <Top>0cm</Top>
            <Left>14.14639cm</Left>
            <Height>0.58867cm</Height>
            <Width>13.28903cm</Width>
            <ZIndex>4</ZIndex>
            <Style>
              <Border>
                <Style>None</Style>
              </Border>
              <PaddingLeft>2pt</PaddingLeft>
              <PaddingRight>2pt</PaddingRight>
              <PaddingTop>2pt</PaddingTop>
              <PaddingBottom>2pt</PaddingBottom>
            </Style>
          </Textbox>
          <Textbox Name=""TextboxDataLabel"">
            <CanGrow>true</CanGrow>
            <KeepTogether>true</KeepTogether>
            <Paragraphs>
              <Paragraph>
                <TextRuns>
                  <TextRun>
                    <Value>Data</Value>
                    <Style>
                      <FontWeight>Bold</FontWeight>
                      <Color>Gray</Color>
                    </Style>
                  </TextRun>
                </TextRuns>
                <Style />
              </Paragraph>
            </Paragraphs>
            <rd:DefaultName>TextboxDataLabel</rd:DefaultName>
            <Top>1.42651cm</Top>
            <Left>0cm</Left>
            <Height>0.58867cm</Height>
            <Width>13.97cm</Width>
            <ZIndex>5</ZIndex>
            <Style>
              <Border>
                <Style>None</Style>
              </Border>
              <PaddingLeft>2pt</PaddingLeft>
              <PaddingRight>2pt</PaddingRight>
              <PaddingTop>2pt</PaddingTop>
              <PaddingBottom>2pt</PaddingBottom>
            </Style>
          </Textbox>
        </ReportItems>
        <Height>4.35681cm</Height>
        <Style>
          <Border>
            <Style>None</Style>
          </Border>
        </Style>
      </Body>
      <Width>27.7cm</Width>
      <Page>
        <PageHeader>
          <Height>1.64382cm</Height>
          <PrintOnFirstPage>true</PrintOnFirstPage>
          <PrintOnLastPage>true</PrintOnLastPage>
          <ReportItems>
            <Textbox Name=""TextboxLegalEntity"">
              <CanGrow>true</CanGrow>
              <KeepTogether>true</KeepTogether>
              <Paragraphs>
                <Paragraph>
                  <TextRuns>
                    <TextRun>
                      <Value>" + this.ReportCompanyProperty + @"</Value>
                      <Style>
                        <FontFamily>Verdana</FontFamily>
                        <FontSize>14pt</FontSize>
                        <FontWeight>Bold</FontWeight>
                        <Color>Gray</Color>
                      </Style>
                    </TextRun>
                  </TextRuns>
                  <Style />
                </Paragraph>
              </Paragraphs>
              <rd:WatermarkTextbox>Title</rd:WatermarkTextbox>
              <rd:DefaultName>TextboxLegalEntity</rd:DefaultName>
              <Height>0.29583in</Height>
              <Width>5.5in</Width>
              <Style>
                <Border>
                  <Style>None</Style>
                </Border>
                <PaddingLeft>2pt</PaddingLeft>
                <PaddingRight>2pt</PaddingRight>
                <PaddingTop>2pt</PaddingTop>
                <PaddingBottom>2pt</PaddingBottom>
              </Style>
            </Textbox>
            <Textbox Name=""ExecutionTime2"">
              <CanGrow>true</CanGrow>
              <KeepTogether>true</KeepTogether>
              <Paragraphs>
                <Paragraph>
                  <TextRuns>
                    <TextRun>
                      <Value>=Globals!ExecutionTime</Value>
                      <Style>
                        <FontSize>8pt</FontSize>
                        <Format>D</Format>
                      </Style>
                    </TextRun>
                  </TextRuns>
                  <Style>
                    <TextAlign>Right</TextAlign>
                  </Style>
                </Paragraph>
              </Paragraphs>
              <rd:DefaultName>ExecutionTime</rd:DefaultName>
              <Left>8.156in</Left>
              <Height>0.5cm</Height>
              <Width>6.71917cm</Width>
              <ZIndex>1</ZIndex>
              <Style>
                <Border>
                  <Style>None</Style>
                </Border>
                <PaddingLeft>2pt</PaddingLeft>
                <PaddingRight>2pt</PaddingRight>
                <PaddingTop>2pt</PaddingTop>
                <PaddingBottom>2pt</PaddingBottom>
              </Style>
            </Textbox>
            <Textbox Name=""ExecutionTime"">
              <CanGrow>true</CanGrow>
              <KeepTogether>true</KeepTogether>
              <Paragraphs>
                <Paragraph>
                  <TextRuns>
                    <TextRun>
                      <Value>=""Page "" &amp; Globals!PageNumber &amp; "" of "" &amp; Globals!TotalPages</Value>
                      <Style>
                        <FontSize>8pt</FontSize>
                        <Format>D</Format>
                      </Style>
                    </TextRun>
                  </TextRuns>
                  <Style>
                    <TextAlign>Right</TextAlign>
                  </Style>
                </Paragraph>
              </Paragraphs>
              <rd:DefaultName>ExecutionTime</rd:DefaultName>
              <Top>0.21074in</Top>
              <Left>8.156in</Left>
              <Height>0.5cm</Height>
              <Width>6.71917cm</Width>
              <ZIndex>2</ZIndex>
              <Style>
                <Border>
                  <Style>None</Style>
                </Border>
                <PaddingLeft>2pt</PaddingLeft>
                <PaddingRight>2pt</PaddingRight>
                <PaddingTop>2pt</PaddingTop>
                <PaddingBottom>2pt</PaddingBottom>
              </Style>
            </Textbox>
            <Textbox Name=""TextboxReportTitle"">
              <CanGrow>true</CanGrow>
              <KeepTogether>true</KeepTogether>
              <Paragraphs>
                <Paragraph>
                  <TextRuns>
                    <TextRun>
                      <Value>" + this.ReportTitleProperty + @"</Value>
                      <Style>
                        <FontWeight>Bold</FontWeight>
                      </Style>
                    </TextRun>
                  </TextRuns>
                  <Style />
                </Paragraph>
              </Paragraphs>
              <rd:DefaultName>TextboxReportTitle</rd:DefaultName>
              <Top>0.82196cm</Top>
              <Left>0cm</Left>
              <Height>0.59748cm</Height>
              <Width>13.97cm</Width>
              <ZIndex>3</ZIndex>
              <Style>
                <Border>
                  <Style>None</Style>
                </Border>
                <PaddingLeft>2pt</PaddingLeft>
                <PaddingRight>2pt</PaddingRight>
                <PaddingTop>2pt</PaddingTop>
                <PaddingBottom>2pt</PaddingBottom>
              </Style>
            </Textbox>
            <Line Name=""Line1"">
              <Top>1.45472cm</Top>
              <Left>0cm</Left>
              <Height>0cm</Height>
              <Width>27.38567cm</Width>
              <ZIndex>4</ZIndex>
              <Style>
                <Border>
                  <Style>Solid</Style>
                </Border>
              </Style>
            </Line>
          </ReportItems>
          <Style>
            <Border>
              <Style>None</Style>
            </Border>
          </Style>
        </PageHeader>
        <PageFooter>
          <Height>1.38112cm</Height>
          <PrintOnFirstPage>true</PrintOnFirstPage>
          <PrintOnLastPage>true</PrintOnLastPage>
          <ReportItems>
            <Textbox Name=""TextboxSecurityPolicy"">
              <CanGrow>true</CanGrow>
              <KeepTogether>true</KeepTogether>
              <Paragraphs>
                <Paragraph>
                  <TextRuns>
                    <TextRun>
                      <Value>Data has been restricted by security policies that may be in effect.</Value>
                      <Style>
                        <FontSize>6pt</FontSize>
                        <Color>Blue</Color>
                      </Style>
                    </TextRun>
                  </TextRuns>
                  <Style>
                    <TextAlign>Right</TextAlign>
                  </Style>
                </Paragraph>
              </Paragraphs>
              <rd:DefaultName>TextboxSecurityPolicy</rd:DefaultName>
              <Top>0.64111cm</Top>
              <Left>20.71625cm</Left>
              <Height>0.5cm</Height>
              <Width>6.71917cm</Width>
              <Style>
                <Border>
                  <Style>None</Style>
                </Border>
                <PaddingLeft>2pt</PaddingLeft>
                <PaddingRight>2pt</PaddingRight>
                <PaddingTop>2pt</PaddingTop>
                <PaddingBottom>2pt</PaddingBottom>
              </Style>
            </Textbox>
            <Textbox Name=""ExecutionTime3"">
              <CanGrow>true</CanGrow>
              <KeepTogether>true</KeepTogether>
              <Paragraphs>
                <Paragraph>
                  <TextRuns>
                    <TextRun>
                      <Value>=User!UserID</Value>
                      <Style>
                        <FontSize>8pt</FontSize>
                        <Format>D</Format>
                      </Style>
                    </TextRun>
                  </TextRuns>
                  <Style>
                    <TextAlign>Right</TextAlign>
                  </Style>
                </Paragraph>
              </Paragraphs>
              <rd:DefaultName>ExecutionTime</rd:DefaultName>
              <Top>0.05556in</Top>
              <Left>8.156in</Left>
              <Height>0.5cm</Height>
              <Width>6.71917cm</Width>
              <ZIndex>1</ZIndex>
              <Style>
                <Border>
                  <Style>None</Style>
                </Border>
                <PaddingLeft>2pt</PaddingLeft>
                <PaddingRight>2pt</PaddingRight>
                <PaddingTop>2pt</PaddingTop>
                <PaddingBottom>2pt</PaddingBottom>
              </Style>
            </Textbox>
          </ReportItems>
          <Style>
            <Border>
              <Style>None</Style>
            </Border>
          </Style>
        </PageFooter>
        <PageHeight>21cm</PageHeight>
        <PageWidth>29.7cm</PageWidth>
        <LeftMargin>1cm</LeftMargin>
        <RightMargin>1cm</RightMargin>
        <TopMargin>1cm</TopMargin>
        <BottomMargin>1cm</BottomMargin>
        <ColumnSpacing>1.27cm</ColumnSpacing>
        <Style />
      </Page>
    </ReportSection>
  </ReportSections>
  <rd:ReportUnitType>Cm</rd:ReportUnitType>
  <rd:ReportServerUrl>http://nimbus2:8080/ReportServer</rd:ReportServerUrl>
  <rd:ReportID>d03dfb1b-e442-458d-a2e2-133e223b14f9</rd:ReportID>
</Report>";
            xml = xml.Replace("<Report xmlns:rd=\"http://schemas.microsoft.com/SQLServer/reporting/reportdesigner\" xmlns:cl=\"http://schemas.microsoft.com/sqlserver/reporting/2010/01/componentdefinition\" xmlns=\"http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition\">", "<Report xmlns:rd=\"http://schemas.microsoft.com/SQLServer/reporting/reportdesigner\" xmlns=\"http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition\">");
            xml = xml.Replace("<ReportSections>", "").Replace("<ReportSection>", "").Replace("</ReportSection>", "").Replace("</ReportSections>", "");

            Stream ret = new MemoryStream(Encoding.UTF8.GetBytes(xml));
            return ret;
        }
    }
}
